<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="匹配路径" v-model="listQuery.path"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="服务ID" v-model="listQuery.serviceId"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="服务地址" v-model="listQuery.url"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="平台名称" v-model="listQuery.apiName"> </el-input>
    <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
    <el-button class="filter-item"  v-if="dynamicRouteManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="序号" width="65">
      <template scope="scope">
          <span>{{scope.row.id}}</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="匹配路径">
      <template scope="scope">
        <span>{{scope.row.path}}</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="服务ID">
      <template scope="scope">
        <span>{{scope.row.serviceId}}</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="服务地址">
      <template scope="scope">
        <span>{{scope.row.url}}</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="服务重试状态">
      <template scope="scope">
        <span v-if="scope.row.retryable === 1">开启</span>
        <span v-if="scope.row.retryable === 0">关闭</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="启用状态">
      <template scope="scope">
        <span v-if="scope.row.enabled === 1">开启</span>
        <span v-if="scope.row.enabled === 0">关闭</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="剥离路径前缀">
      <template scope="scope">
        <span v-if="scope.row.stripPrefix === 1">开启</span>
        <span v-if="scope.row.stripPrefix === 0">关闭</span>
      </template>
    </el-table-column>
    <el-table-column width="200" align="center" label="路径名称">
      <template scope="scope">
        <span>{{scope.row.apiName}}</span>
      </template>
    </el-table-column>
    <el-table-column align="center" label="操作" width="200"> <template scope="scope">
        <el-button v-if="dynamicRouteManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑</el-button>
        <el-button v-if="dynamicRouteManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>


  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="120px">
      <el-form-item label="匹配路径" prop="path">
        <el-input v-model="form.path" placeholder="请输入匹配路径"></el-input>
      </el-form-item>
      <el-form-item label="服务ID" prop="serviceId">
        <el-input v-model="form.serviceId" placeholder="请输入服务ID"></el-input>
      </el-form-item>
      <el-form-item label="服务地址" prop="url">
        <el-input v-model="form.url" placeholder="请输入服务地址"></el-input>
      </el-form-item>
      <el-form-item label="服务重试" prop="retryable">
        <el-select v-model="form.retryable" placeholder="请选择服务重试状态">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="enabled">
        <el-select v-model="form.enabled" placeholder="请选择启用状态">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="剥离路径前缀" prop="stripPrefix">
        <el-select v-model="form.stripPrefix" placeholder="请选择剥离路径前缀">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="路径名称" prop="apiName">
        <el-input v-model="form.apiName" placeholder="请输入路径名称"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus == 'create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import { page, addObj, delObj, putObj } from '@/api/admin/dynamicRouting/index'
import { mapGetters } from 'vuex'
export default {
  name: 'user',
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        path: undefined,
        serviceId: undefined,
        url: undefined,
        apiName: undefined
      },
      listLoading: true,
      list: null,
      total: null,
      form: {
        path: undefined,
        serviceId: undefined,
        url: undefined,
        retryable: undefined,
        enabled: undefined,
        stripPrefix: undefined,
        apiName: undefined
      },
      options: [{
        value: 1,
        label: '开启'
      }, {
        value: 0,
        label: '关闭'
      }
      ],
      dialogFormVisible: false,
      rules: {
        path: [
          {
            required: true,
            message: '请输入匹配路径',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        retryable: [
          {
            required: true,
            message: '请选择服务重试状态',
            trigger: 'blur'
          }
        ],
        enabled: [
          {
            required: true,
            message: '请选择路由状态',
            trigger: 'blur'
          }
        ],
        stripPrefix: [
          {
            required: true,
            message: '请选择剥离路径前缀状态',
            trigger: 'blur'
          }
        ]
      },
      dynamicRouteManager_btn_edit: false,
      dynamicRouteManager_btn_del: false,
      dynamicRouteManager_btn_add: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogStatus: '',
      tableKey: 0
    }
  },
  created() {
    this.getList()
    this.dynamicRouteManager_btn_edit = this.elements['dynamicRouteManager:btn_edit']
    this.dynamicRouteManager_btn_del = this.elements['dynamicRouteManager:btn_del']
    this.dynamicRouteManager_btn_add = this.elements['dynamicRouteManager:btn_add']
  },
  computed: {
    ...mapGetters([
      'elements',
      'token',
      'name',
      'userId'
    ])
  },
  watch: {
  },
  methods: {
    getList() {
      this.listLoading = true
      page(this.listQuery)
        .then(response => {
          console.log('=================================')
          console.log(response)
          this.list = response.body.data.rows
          this.total = response.body.data.total
          this.listLoading = false
        })
    },
    handleFilter() {
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.form = row
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delObj(row.id)
          .then(response => {
            if (response.code === '0000') {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            } else {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'failed',
                duration: 2000
              })
            }
          })
      })
    },
    create(formName) {
      console.log('=====formName==========================================')
      console.log(formName)
      const set = this.$refs
      this.form.token = this.token
      this.form.platformLevel = '02'
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form)
            .then(response => {
              console.log(response)
              if (response.code === '0000') {
                this.dialogFormVisible = false
                this.getList()
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                })
              } else {
                this.$notify({
                  title: '失败',
                  message: response.message,
                  type: 'failed',
                  duration: 2000
                })
              }
            })
        } else {
          return false
        }
      })
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          putObj(this.form).then(response => {
            console.log(response)
            if (response.code === '0000') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'failed',
                duration: 2000
              })
            }
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    resetTemp() {
      this.form = {
        path: undefined,
        serviceId: undefined,
        url: undefined,
        retryable: undefined,
        enabled: undefined,
        stripPrefix: undefined,
        apiName: undefined
      }
    }
  }
}
</script>
<style>
.el-dialog {
  width: 1200px !important;
}
</style>